using System;
using System.Xml;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;


/// <summary>
/// Summary description for GameService
/// </summary>
/// 
namespace plzHalp
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class GameService : System.Web.Services.WebService
    {

        public GameService()
        {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }


        [WebMethod(EnableSession = true)]
        [System.Xml.Serialization.XmlInclude(typeof(EntityWanderer))]
        [System.Xml.Serialization.XmlInclude(typeof(EntityPlayer))]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public List<GameEntity> GetEntities()
        {
            List<GameEntity> returnEntities = new List<GameEntity>();
            // Now let's add all our game entities
            foreach (GameEntity myEnt in GameWorld.getEntities())
            {
                // We'll want to add every entity on the current map 
                // or optionally all of those within sight of the player

                if (myEnt == ((EntityPlayer)Session["EntityPlayer"]))
                {
                    returnEntities.Add((EntityPlayer)myEnt.Clone());
                    returnEntities[returnEntities.Count - 1].eType = 4;

                }
                else
                {
                    returnEntities.Add(myEnt);
                }
            }
            return returnEntities;
        }
        


        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet=true)]
        public List<String> GetMap(int mapNum)
        {
            return GameWorld.getMap(mapNum);
        }

        [WebMethod(EnableSession = true)]
        public List<string> GetIRCText()
        {
            return IrcManager.ircText;
        }

        [WebMethod(EnableSession = true)]
        public void SendIRCMessage(string message)
        {
            IrcManager.ircText.Add(message);
            IrcManager.askHAL(message);
            IrcManager.SendIRCMessage(message);
        }


        /// <summary>
        /// This function receives updates from the player with their current position
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        [WebMethod(EnableSession = true)]
        public void UpdatePlayerPos(int x, int y)
        {
            if (Session["EntityPlayer"] != null)
            {
                ((EntityPlayer) Session["EntityPlayer"]).X = x;
                ((EntityPlayer) Session["EntityPlayer"]).Y = y;
            }
        }
    }
}

